package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo10 {
    public static void main(String[] args) {
        System.out.println("修改用户密码");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入新密码：");
        String password = sc.nextLine();

        try (Connection connection = DBUtil.getConnection();){
            String sql = "UPDATE user SET password=? \n" +
                    "WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,username);


            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "修改成功" : "修改失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
